package com.fuelwallet.view;

import java.util.ArrayList;
import java.util.List;

import com.fuelwallet.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.fuelwallet.model.CarProfile;
import com.fuelwallet.model.CarProfileHandle;
import com.fuelwallet.model.FuelPrice;
import com.fuelwallet.model.FuelPriceHandle;

public class AddProfileActivity extends Activity {
	private CarProfileHandle profiles;
	private FuelPriceHandle prices;
	EditText vAccountName;
	EditText vDisplacement;
	private String fuelName;
	private String account_name;
	
	public void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState); 
		setContentView(R.layout.add_profile);
		setTitle("Create New Account");
		
		Bundle bundle = getIntent().getExtras();
		account_name = bundle.getString("account_name");
		
		vAccountName = (EditText) findViewById(R.id.account_name);
        vDisplacement = (EditText) findViewById(R.id.displacement);
		
        profiles = new CarProfileHandle(this);
		prices = new FuelPriceHandle(this); 
		prices.open();
		
		Spinner spinner = (Spinner) findViewById(R.id.spinner);
		List <String>fuelnameList=prices.getFuelNameList();
		List <String>displayList=new ArrayList<String>();
		for(int i=0;i<fuelnameList.size();i++){
			FuelPrice fuelpriceTemp=prices.getFuelPrice(fuelnameList.get(i));
			displayList.add(fuelpriceTemp.getfuelName());
		}
	    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
	    		android.R.layout.simple_spinner_item, displayList);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spinner.setAdapter(adapter);
	    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
	    
	    if(!account_name.equals("")){
        	vAccountName.setText(account_name);
        	vAccountName.setEnabled(false);
        	profiles.open();
        	vDisplacement.setText(Float.toString(profiles.getCarProfile(account_name).getDisplacement()));
        	spinner.setSelection(prices.getFuelNameList().indexOf(profiles.getCarProfile(account_name).getFuelName()));
        	fuelName = profiles.getCarProfile(account_name).getFuelName();
        	profiles.close();
        }
	    
	    prices.close();
	}
	
	public void onBackPressed(){		
		super.onBackPressed();
		overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
	}
	
	public void onClick(View view) {
    	switch (view.getId()) {
    	case R.id.submit:
    		if(inputCheck()){
	            profiles.open();
	            String new_account_name = vAccountName.getText().toString();
	            String displacement = vDisplacement.getText().toString();
	            float disp = Float.parseFloat(displacement);
	            CarProfile newCarProfile = new CarProfile(new_account_name, Float.parseFloat(String.format("%.1f", disp)),
	            		fuelName);
	            if(!account_name.equals("")){
	            	profiles.deleteCarProfile(account_name);
	            	profiles.putCarProfile(newCarProfile);
	            	this.finish();
	            	overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
	            }else if(profiles.getAccountNameList().indexOf(new_account_name)==-1){
	            	profiles.putCarProfile(newCarProfile);
	            	this.finish();
	            	overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
	            }else
	            	alertInvalid("Account Name has alreay existed!!");
	            profiles.close();
	    	}
            break;
    	case R.id.clear:
    		if(account_name.equals(""))
    			vAccountName.setText("");
    		vDisplacement.setText("");
    		break;
    	}
    }
	
	public boolean inputCheck(){
		if((vAccountName.length()==0)||(vDisplacement.length()==0)){
			alertInvalid("Please input correct Account Name and Displacement!!");
			return false;
		}
		else
			return true;
	}
	
	public void alertInvalid(String msg){
	    	Builder MyAlertDialog = new AlertDialog.Builder(this);
	    	MyAlertDialog.setTitle("Invalid Input");
	    	MyAlertDialog.setMessage(msg);
	    	DialogInterface.OnClickListener OkClick = new DialogInterface.OnClickListener()
	    	{
	    		public void onClick(DialogInterface dialog, int which) {
	    			dialog.cancel();
	    		}
	    	};
	    	MyAlertDialog.setNeutralButton("OK",OkClick );
	    	MyAlertDialog.show(); 	
	}
	
	public class MyOnItemSelectedListener implements OnItemSelectedListener {

	    public void onItemSelected(AdapterView<?> parent,
	        View view, int pos, long id) {
	    	fuelName = parent.getItemAtPosition(pos).toString();
	    }

	    public void onNothingSelected(AdapterView parent) {
	      // Do nothing.
	    }
	}
}